<template>
	<view class="dog-food-container">
		<!-- 返回按钮 -->
		<view class="back-button" @click="goBack">
			<text class="back-arrow">&#10094;</text>
		</view>
		
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="title">狗粮专区</view>
		</view>
		
		<!-- 搜索框 -->
		<view class="search-box">
			<view class="search-input">
				<view class="search-icon-container">
					<text class="search-icon">&#9906;</text>
				</view>
				<input type="text" placeholder="请输入搜索的商品" disabled @click="navigateToSearch" />
				<view class="search-btn" @click="navigateToSearch">搜索</view>
			</view>
		</view>
		
		<!-- 筛选栏 -->
		<view class="filter-bar">
			<view class="filter-item" :class="{'active': activeFilter === 'comprehensive'}" @click="setFilter('comprehensive')">
				<text>综合</text>
			</view>
			<view class="filter-item" :class="{'active': activeFilter === 'sales'}" @click="setFilter('sales')">
				<text>销量</text>
			</view>
			<view class="filter-item" :class="{'active': activeFilter === 'new'}" @click="setFilter('new')">
				<text>新品</text>
			</view>
			<view class="filter-item" :class="{'active': activeFilter === 'price'}" @click="setFilter('price')">
				<text>价格</text>
				<view class="price-sort-buttons">
					<view class="sort-button up" :class="{'active': priceSort === 'asc'}">&#9650;</view>
					<view class="sort-button down" :class="{'active': priceSort === 'desc'}">&#9660;</view>
				</view>
			</view>
			<view class="filter-item" :class="{'active': activeFilter === 'filter'}" @click="setFilter('filter')">
				<text>筛选</text>
				<view class="filter-icon-container">
					<text class="filter-icon-line"></text>
					<text class="filter-icon-line"></text>
					<text class="filter-icon-line"></text>
				</view>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<view class="product-list">
			<view class="product-item" v-for="(item, index) in productList" :key="index" @click="navigateToDetail(item.id)">
				<view class="product-image">
					<image :src="item.image" mode="aspectFill"></image>
				</view>
				<view class="product-info">
					<view class="product-name">{{item.name}}</view>
					<view class="product-sales">已售{{item.sales}}件</view>
					<view class="product-price-row">
						<view class="product-price">
							<text class="price-symbol">¥</text>
							<text class="price-value">{{item.price}}</text>
							<text class="price-decimal" v-if="item.price % 1 !== 0">.{{(item.price % 1).toFixed(2).substring(2)}}</text>
							<text class="original-price" v-if="item.originalPrice">¥{{item.originalPrice}}</text>
						</view>
						<view class="add-cart">
							<image src="/static/add-cart-btn.png" mode="aspectFit"></image>
						</view>
					</view>
					<view class="product-tags">
						<view class="product-tag" v-if="item.promotion">{{item.promotion}}</view>
						<view class="product-tag" v-if="item.freeShipping">满97包邮</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activeFilter: 'comprehensive',
				priceSort: '', // 可以是 'asc' 或 'desc' 或空字符串
				productList: [
					{
						id: 1,
						name: '皇家狗粮幼犬专用粮S26 2kg',
						price: 125.8,
						originalPrice: 159.9,
						sales: 5201,
						image: '/static/狗狗专区.png',
						promotion: '买2送1',
						freeShipping: true
					},
					{
						id: 2,
						name: '冠能狗粮成犬全价粮2.5kg',
						price: 109.9,
						originalPrice: 129.9,
						sales: 4871,
						image: '/static/狗狗专区.png',
						promotion: '满3减10',
						freeShipping: true
					},
					{
						id: 3,
						name: '爱肯拿无谷鸡肉狗粮成犬粮2kg',
						price: 235.5,
						sales: 2893,
						image: '/static/狗狗专区.png',
						freeShipping: true
					},
					{
						id: 4,
						name: '比瑞吉天然粮泰迪专用狗粮2kg',
						price: 189,
						originalPrice: 199,
						sales: 3054,
						image: '/static/狗狗专区.png',
						freeShipping: true
					}
				]
			}
		},
		onLoad(options) {
			this.loadProductData();
		},
		methods: {
			loadProductData() {
				let params = {
					categoryId: 101, // 狗狗主粮分类ID
					page: 1,
					size: 20
				};

				if (this.activeFilter === 'sales') {
					params.sortBy = 'sales';
					params.order = 'desc';
				} else if (this.activeFilter === 'price') {
					params.sortBy = 'price';
					params.order = this.priceSort || 'asc';
				}

				this.$api.productApi.getList(params)
					.then(res => {
						if (res && res.data) {
							this.productList = res.data;
						}
					})
					.catch(err => {
						console.error("获取狗狗主粮失败", err);
						uni.showToast({
							title: '商品加载失败',
							icon: 'none'
						});
					});
			},
			goBack() {
				uni.navigateBack()
			},
			navigateToSearch() {
				uni.navigateTo({
					url: '/pages/search/index'
				})
			},
			setFilter(filter) {
				if(filter === 'price' && this.activeFilter === 'price') {
					// 如果已经是价格筛选，则切换排序方向
					if(this.priceSort === 'asc') {
						this.priceSort = 'desc'
					} else if(this.priceSort === 'desc') {
						this.priceSort = ''
					} else {
						this.priceSort = 'asc'
					}
				} else {
					this.activeFilter = filter
					if(filter !== 'price') {
						this.priceSort = ''
					} else if(this.priceSort === '') {
						this.priceSort = 'asc'
					}
				}
				// 根据筛选条件重新加载数据
				this.loadProductData()
			},
			navigateToDetail(id) {
				uni.navigateTo({
					url: '/pages/shop/product-detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.dog-food-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		position: relative;
		
		.back-button {
			position: absolute;
			top: 40rpx;
			left: 30rpx;
			z-index: 10;
			width: 40rpx;
			height: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			
			.back-arrow {
				font-size: 36rpx;
				color: #666;
				font-weight: normal;
			}
		}
		
		.header {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 30rpx;
			background-color: #fff;
			position: relative;
			
			.title {
				font-size: 36rpx;
				font-weight: bold;
			}
		}
		
		.search-box {
			padding: 20rpx 30rpx;
			background-color: #fff;
			
			.search-input {
				display: flex;
				align-items: center;
				background-color: #f5f5f5;
				border-radius: 30rpx;
				padding: 10rpx 20rpx;
				
				.search-icon-container {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-right: 10rpx;
					
					.search-icon {
						font-size: 28rpx;
						color: #999;
						transform: rotate(-45deg);
						display: inline-block;
					}
				}
				
				input {
					flex: 1;
					height: 60rpx;
					font-size: 28rpx;
				}
				
				.search-btn {
					background-color: #ff6600;
					color: #fff;
					padding: 10rpx 30rpx;
					border-radius: 30rpx;
					font-size: 28rpx;
					margin-left: 10rpx;
				}
			}
		}
		
		.filter-bar {
			display: flex;
			background-color: #fff;
			border-bottom: 1px solid #eee;
			
			.filter-item {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: center;
				height: 80rpx;
				font-size: 28rpx;
				color: #333;
				
				&.active {
					color: #ff6600;
					font-weight: bold;
				}
				
				.price-sort-buttons {
					display: flex;
					flex-direction: column;
					margin-left: 6rpx;
					height: 30rpx;
					
					.sort-button {
						font-size: 16rpx;
						line-height: 14rpx;
						color: #ccc;
						
						&.active {
							color: #ff6600;
						}
						
						&.up {
							margin-bottom: 2rpx;
						}
					}
				}
				
				.filter-icon-container {
					display: flex;
					flex-direction: column;
					margin-left: 6rpx;
					width: 20rpx;
					height: 20rpx;
					justify-content: space-between;
					
					.filter-icon-line {
						height: 2rpx;
						background-color: currentColor;
						width: 100%;
						border-radius: 2rpx;
						
						&:first-child {
							width: 100%;
						}
						
						&:nth-child(2) {
							width: 75%;
							align-self: flex-end;
						}
						
						&:last-child {
							width: 50%;
							align-self: flex-end;
						}
					}
				}
			}
		}
		
		.product-list {
			padding: 20rpx;
			
			.product-item {
				display: flex;
				background-color: #fff;
				border-radius: 20rpx;
				padding: 20rpx;
				margin-bottom: 20rpx;
				
				.product-image {
					width: 200rpx;
					height: 200rpx;
					margin-right: 20rpx;
					
					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
				
				.product-info {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					
					.product-name {
						font-size: 28rpx;
						line-height: 1.4;
						margin-bottom: 10rpx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					
					.product-sales {
						font-size: 24rpx;
						color: #999;
						margin-bottom: 10rpx;
					}
					
					.product-price-row {
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-bottom: 10rpx;
						
						.product-price {
							display: flex;
							align-items: baseline;
							
							.price-symbol {
								font-size: 24rpx;
								color: #ff6600;
							}
							
							.price-value {
								font-size: 36rpx;
								font-weight: bold;
								color: #ff6600;
							}
							
							.price-decimal {
								font-size: 24rpx;
								color: #ff6600;
							}
							
							.original-price {
								font-size: 24rpx;
								color: #999;
								text-decoration: line-through;
								margin-left: 10rpx;
							}
						}
						
						.add-cart {
							width: 60rpx;
							height: 60rpx;
							
							image {
								width: 100%;
								height: 100%;
							}
						}
					}
					
					.product-tags {
						display: flex;
						flex-wrap: wrap;
						
						.product-tag {
							font-size: 22rpx;
							color: #ff6600;
							background-color: #fff5f0;
							padding: 4rpx 10rpx;
							border-radius: 6rpx;
							margin-right: 10rpx;
							margin-bottom: 6rpx;
						}
					}
				}
			}
		}
	}
</style> 